package com.matrix.yukun.matrix.tool_module.battery.presenter;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.BatteryManager;
import android.util.Log;

import com.matrix.yukun.matrix.chat_module.mvp.BasePresenter;
import com.matrix.yukun.matrix.chat_module.mvp.BaseView;
import com.matrix.yukun.matrix.chat_module.mvp.ChatControler;

import java.text.DecimalFormat;

/**
 * 作者：Administrator
 * 日期：2023/10/16 10:02
 * 描述：
 *
 * @author Administrator
 */
public class BatteryPresenter extends BatteryContract.Presenter {


    public BatteryPresenter(Context context, BatteryContract.View view) {
        super(context, view);
    }

    /**
     * Method for getting the current battery percentage
     *
     * @return
     */
    public int getBatteryPercent(Context context) {
        IntentFilter ifilter = new IntentFilter(Intent.ACTION_BATTERY_CHANGED);
        Intent batteryStatus = context.registerReceiver(null, ifilter);
        int level = batteryStatus.getIntExtra(BatteryManager.EXTRA_LEVEL, -1);
        return level;
    }

    /**
     * Method for getting the total battery capacity
     *
     * @return
     */
    public double getBatteryCapacity(Activity activity) {
        double battCapacity = 0.0d;
        Object mPowerProfile_ = null;

        Log.d("Debug", "in getBatteryCapacity()");
        final String POWER_PROFILE_CLASS = "com.android.internal.os.PowerProfile";

        try {
            mPowerProfile_ = Class.forName(POWER_PROFILE_CLASS)
                    .getConstructor(Context.class).newInstance(activity);
        } catch (Exception e) {
            e.printStackTrace();
            Log.d("Debug", "Try 1 - Exception e: " + e.toString());
        }
        try {
            battCapacity = (Double) Class
                    .forName(POWER_PROFILE_CLASS)
                    .getMethod("getAveragePower", String.class)
                    .invoke(mPowerProfile_, "battery.capacity");
            Log.d("Debug", "battCapacity is now: " + battCapacity);
        } catch (Exception e) {
            e.printStackTrace();
            Log.d("Debug", "Try 2 - Exception e: " + e.toString());
        }
        Log.d("Debug", "Leaving getBatteryCapacity()");
        return battCapacity;
    }

    // Method for getting the exact current battery level
    public int getExactBattery(Context context) {
        Log.d("Debug", "In getExactBattery()");
        float batteryPercentage = getBatteryPercent(context);
        Log.d("Debug", "batteryPercentage = " + batteryPercentage);
        double totalCapacity = getBatteryCapacity((Activity) context);
        Log.d("Debug", "totalCapacity = " + totalCapacity);
        int currLevel = (int) (totalCapacity * batteryPercentage) / 100;
        Log.d("Debug", "currLevel = " + currLevel);
        return currLevel;
    }

    public double numberFormat(double numberDecimal) {
        DecimalFormat format = new DecimalFormat("#.###");
        String numberStr = format.format(numberDecimal);
        return Double.parseDouble(numberStr);
    }
}
